package com.zsyh.jiujiu.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.utils.StatusUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class TimeSelectDialog extends DialogFragment implements ClallBackData {

    @BindView(R.id.quxiao)
    TextView quxiao;
    @BindView(R.id.queding)
    TextView queding;
    @BindView(R.id.fragmentlayout)
    FrameLayout fragmentlayout;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.reck)
    RelativeLayout reck;
    private Unbinder unbinder;
    private ClallBackData clallBackData;
    private TimePickerView pvTime;
    private String mytime;
    private boolean isshow;

    public TimeSelectDialog(boolean isshow) {
        this.isshow = isshow;
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_time, container, false);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        unbinder = ButterKnife.bind(this, view);
        initTimePicker();
        if (isshow == false) {
            reck.setVisibility(View.GONE);
        }
        return view;
    }

    public void setSelectAddresFinish(ClallBackData clallBackData) {
        this.clallBackData = clallBackData;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels, dm.heightPixels - StatusUtil.getStatusBarHeight(getActivity()));
    }


    @Override
    public void getData(String data, String name, String number) {

    }

    @OnClick({R.id.quxiao, R.id.queding, R.id.checkbox})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.quxiao:
                dismiss();
                break;
            case R.id.queding:
                clallBackData.getData(mytime, "", "");
                dismiss();
                break;
            case R.id.checkbox:
                mytime = "永久";
                break;
        }
    }

    private void initTimePicker() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(1949, 01, 01);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2021, 3, 10);
        //时间选择器
        pvTime = new TimePickerBuilder(getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                mytime = getTime(date);
                clallBackData.getData(mytime, "", "");
                dismiss();
            }
        })

                .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {

                    @Override
                    public void customLayout(View v) {
                        TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        TextView iv_cancel = (TextView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvTime.returnData();
                                if (checkbox.isChecked()) {
                                    clallBackData.getData("永久", "", "");
                                } else {
                                    clallBackData.getData(mytime, "", "");
                                }

                                dismiss();
                            }
                        });
                        iv_cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                clallBackData.getData(mytime, "", "");
                                dismiss();
                            }
                        });

                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                //.setLabel("", "", "", "", "", "") //设置空字符串以隐藏单位提示   hide label
                //.setDividerColor(Color.DKGRAY)
                //.setContentTextSize(20)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setDecorView(fragmentlayout)//非dialog模式下,设置ViewGroup, pickerView将会添加到这个ViewGroup中
                .setOutSideColor(0x00000000)
                .setOutSideCancelable(false)
                .build();

        pvTime.setKeyBackCancelable(false);//系统返回键监听屏蔽掉
        pvTime.show();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}
